package com.qiangesoft.batchsave.controller;

import com.qiangesoft.batchsave.entity.MallOrder;
import com.qiangesoft.batchsave.service.IMallOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/order")
public class MallOrderController {

    @Autowired
    private IMallOrderService mallOrderService;

    @GetMapping("/save1")
    public void save1() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // mybatis-plus
        long start = System.currentTimeMillis();
        mallOrderService.saveBatch(orderList);
        System.out.println("mybatis-plus的【savaBatch】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save2")
    public void save2() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // xml
        long start = System.currentTimeMillis();
        mallOrderService.saveBatchXml(orderList);
        System.out.println("mybatis-plus的【xml拼接sql】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save3")
    public void save3() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // insertBatchSomeColumn
        long start = System.currentTimeMillis();
        mallOrderService.insertBatchSomeColumn(orderList);
        System.out.println("mybatis-plus的【insertBatchSomeColumn】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save4")
    public void save4() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // 手动提交
        long start = System.currentTimeMillis();
        mallOrderService.manualCommit(orderList);
        System.out.println("sqlSession的【手动提交】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save5")
    public void save5() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // 循环插入
        long start = System.currentTimeMillis();
        for (MallOrder mallOrder : orderList) {
            mallOrderService.save(mallOrder);
        }
        System.out.println("mybatis-plus的【循环】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save6")
    public void save6() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // statement.executeBatch
        long start = System.currentTimeMillis();
        mallOrderService.executeBatch(orderList);
        System.out.println("statement的【executeBatch】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    @GetMapping("/save7")
    public void save7() {
        // 数据准备
        List<MallOrder> orderList = getMallOrderList();

        // 异步任务处理
        long start = System.currentTimeMillis();
        mallOrderService.saveBatchAsync(orderList);
        System.out.println("mybatis-plus的【异步任务处理】插入数据" + orderList.size() + "条，耗时：" + (System.currentTimeMillis() - start) + "ms");
    }

    /**
     * 构建数据
     *
     * @return
     */
    private static List<MallOrder> getMallOrderList() {
        List<MallOrder> orderList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            MallOrder mallOrder = new MallOrder();
            mallOrder.setCustomerId(1L);
            mallOrder.setOrderStatus(1);
            mallOrder.setPaymentMethod(1);
            mallOrder.setTotalAmount(BigDecimal.valueOf(12));
            mallOrder.setShippingFee(BigDecimal.valueOf(1));
            mallOrder.setCouponDiscount(BigDecimal.valueOf(0));
            mallOrder.setOrderDate(LocalDateTime.now());
            mallOrder.setPaymentTime(LocalDateTime.now());
            mallOrder.setShippingAddress("哈哈");
            mallOrder.setReceiverName("暂时");
            mallOrder.setReceiverPhone("13211111111");
            orderList.add(mallOrder);
        }
        return orderList;
    }

}

